﻿using System;
using System.Collections.Generic;
using System.Text;

using RaisingStudio.Data.Common;
using RaisingStudio.Data.Common.Factories;

namespace RaisingStudio.Data.Common
{
    public class DataTransactionBase : ITransactionManager
    {
        #region Common property 
        protected System.Data.IDbConnection connection;
        /// <summary>
        /// database connection.
        /// </summary>
        public System.Data.IDbConnection Connection
        {
            get
            {
                return this.connection;
            }
        }

        private DatabaseType _databaseType;
        /// <summary>
        /// Database type.
        /// </summary>
        public DatabaseType DatabaseType
        {
            get
            {
                return this._databaseType;
            }
            set
            {
                this._databaseType = value;
            }
        }

        private string _providerName;
        /// <summary>
        /// The name of provider.
        /// </summary>
        public string ProviderName
        {
            get
            {
                return this._providerName;
            }
            set
            {
                this._providerName = value;
            }
        }

        protected TransactionManager transactionManager;
        /// <summary>
        /// database transaction manager.
        /// </summary>
        public TransactionManager TransactionManager
        {
            get
            {
                return this.transactionManager;
            }
        }
        #endregion
        #region Constructor
        /// <summary>
        /// Constructor
        /// </summary>
        public DataTransactionBase()
        {
            this.connection = ConnectionFactory.CreateConnection();

            DatabaseType databaseType;
            string providerName;
            ConnectionFactory.GetConnectionSetting(out databaseType, out providerName);
            this._databaseType = databaseType;
            this._providerName = providerName;
            
            this.transactionManager = TransactionManagerFactory.CreateTransactionManager(this.connection);
        }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="connection">database connection.</param>
        public DataTransactionBase(System.Data.IDbConnection connection)
        {
            this.connection = connection;
            this.transactionManager = TransactionManagerFactory.CreateTransactionManager(this.connection);
        }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="transactionManager">database transaction manager.</param>
        public DataTransactionBase(TransactionManager transactionManager)
        {
            this.transactionManager = transactionManager;
            this.connection = transactionManager.Connection;
        }
        
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="connectionName">the name of connection.</param>
        public DataTransactionBase(string connectionName)
        {
            this.connection = ConnectionFactory.CreateConnection(connectionName);

            DatabaseType databaseType;
            string providerName;
            ConnectionFactory.GetConnectionSetting(connectionName, out databaseType, out providerName);
            this._databaseType = databaseType;
            this._providerName = providerName;

            this.transactionManager = TransactionManagerFactory.CreateTransactionManager(this.connection);
        }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="databaseType">database type.</param>
        /// <param name="connectionString">connection string.</param>
        public DataTransactionBase(DatabaseType databaseType, string connectionString)
        {
            this.connection = ConnectionFactory.CreateConnection(databaseType, connectionString);
            this._databaseType = databaseType;

            this.transactionManager = TransactionManagerFactory.CreateTransactionManager(this.connection);
        }

        public DataTransactionBase(string providerName, string connectionString)
        {
            this.connection = ConnectionFactory.CreateConnection(providerName, connectionString);
            this._providerName = providerName;

            this.transactionManager = TransactionManagerFactory.CreateTransactionManager(this.connection);
        }
        #endregion

        #region Transactions
        /// <summary>
        /// Begin Database transaction.
        /// </summary>
        /// <returns>The ID of transaction.</returns>
        public virtual string BeginTransaction()
        {
            return this.transactionManager.BeginTransaction();
        }

        /// <summary>
        /// Begin Database transaction.
        /// </summary>
        /// <param name="isolationLevel">Specifies the isolation level for the transaction.</param>
        /// <returns>The ID of transaction.</returns>
        public virtual string BeginTransaction(System.Data.IsolationLevel isolationLevel)
        {
            return this.transactionManager.BeginTransaction(isolationLevel);
        }

        /// <summary>
        /// Commit Database transaction.
        /// </summary>
        public virtual void CommitTransaction()
        {
            this.transactionManager.CommitTransaction();
        }

        /// <summary>
        /// Commit Database transaction.
        /// </summary>
        /// <param name="transactionID">The ID of transaction.</param>
        public virtual void CommitTransaction(string transactionID)
        {
            this.transactionManager.CommitTransaction(transactionID);
        }

        /// <summary>
        /// Rollback transaction.
        /// </summary>
        public virtual void RollbackTransaction()
        {
            this.transactionManager.RollbackTransaction();
        }

        /// <summary>
        /// Rollback transaction.
        /// </summary>
        /// <param name="transactionID">The ID of transaction.</param>
        public virtual void RollbackTransaction(string transactionID)
        {
            this.transactionManager.RollbackTransaction(transactionID);
        }
        #endregion
    }
}
